import gtk
import os
import blabla
import ui

class mainGUI:
    def __init__(self,modulePath):
        self._modulePath = modulePath
        #init UI
        self.builder = gtk.Builder()
        self.builder.add_from_file(os.path.join(self._modulePath,"ui/main.glade"))
        self.builder.connect_signals(self)
        #get ref to ui objects
        self._threadsVbox = self.builder.get_object("threadsVbox")
        self._newTitleEntry = self.builder.get_object("newTitleEntry")
        self._newTextEntry = self.builder.get_object("newTextEntry")
        self._newBlablaFriendsEntry = self.builder.get_object("newBlablaFriendsEntry")
        #get a blabla factory instance
        self._blablaFactory = blabla.blablaFactory()
        #class varibales
        self._newBlablaFriends = None
        #show thread list
        self.refreshThreadList()
        
    def on_selectFriendsAction_activate(self,widget,data=None):
        self._newBlablaFriends = ui.selectFriends()
        sfr = ""
        if self._newBlablaFriends:
            for f in self._newBlablaFriends:
                sfr += f.getName() + ", "
        self._newBlablaFriendsEntry.set_text(sfr)
    
    def getMainObject(self):
        return self.builder.get_object("mainHbox")
    
    def refreshThreadList(self):
        #clean list
        for widget in self._threadsVbox.get_children():
            self._threadsVbox.remove(widget)
        #get list of threads
        threadsList = self._blablaFactory.getListBlaBlas()
        for t in threadsList:
            self._threadsVbox.pack_start(gtk.Label(t.getTitle()),False,True)
            self._threadsVbox.show_all()
        
    def on_addThreadAction_activate(self,widget,data=None):
        #check input datas
        if (not self._newTitleEntry.get_text()) or (not self._newTextEntry.get_text()):
            return #TODO : handle error
        #add a new blabla own entry
        self._blablaFactory.createBlaBla(self._newTitleEntry.get_text(),self._newTextEntry.get_text(),self._newBlablaFriends)
        self.refreshThreadList()
        